<?php

namespace Admin\Controller;

use Think\Controller;
use Common\Service\ApiService;  //接口服务调用api
class NoticeController extends CommonController {
	/**
	 * 通知列表 
	 */
	public function index() {
// 		$url="http://c.b1wv.com/h.iUXWbh?cv=ZGECZEuqPs6&sm=abd58c";
// 		$rurl=$this->get_redirect_url($url);
// 		echo $rurl;
// 		exit;
		$title=I('title');
		if($title)
		{
			$list = $this->lists("Notice", ['status' => ['egt', 0],'title'=>['like','%'.$title.'%']]);
			$this->assign('title',$title);
		}  else {
			$list = $this->lists("Notice", ['status' => ['egt', 0]]);
		}
	
		$this->assign("_list", $list);
		Cookie('__forward__', $_SERVER['REQUEST_URI']);
		$this->meta_title = '页面管理';
		$this->display();
	}
	
	/**
	 * 添加
	 */
	public function add() {
	
		if (IS_POST) {
			$singlePage = D("Notice");
			if ($singlePage->create()) {
				if ($singlePage->add()) {
					$this->success("操作成功",Cookie('__forward__'));
				} else {
					$this->error("操作失败");
				}
			} else {
				$this->error($singlePage->getError());
			}
		}
		$index_path = C("index_tmpl_path");
		$index_path = $index_path . CONTROLLER_NAME . "/*";
		 
		/** 获取模板文件 * */
		$theme_path_tmpl = array_map("basename", glob($index_path));
		$this->assign("theme_path_tmpl", $theme_path_tmpl);
		$this->meta_title = '页面添加';
	
		$this->display();
	}
	/**
	 * 改变数据库操作
	 */
	public function changestatus() {
		$method = I("method");
		$id = I("id", 0);
		$status = I("status");
		if ($status == 1) {
			$status = 0;
		} else {
			$status = 1;
		}
		switch ($method) {
			case "cgStatus": //审核状态
				$data["status"] = $status;
				return $this->changeFieldData("Notice", array("id" => $id), $data);
				break;
			case "cgIstop" :  //是否置顶
				$data["istop"] = $status;
	
				return $this->changeFieldData("Notice", array("id" => $id), $data);
				break;
			case "cgRecommended" :  //是否推荐
				$data["recommended"] = $status;
				return $this->changeFieldData("Notice", array("id" => $id), $data);
				break;
	
			case "deleteArticle" :  //删除文章
				return $this->delete("Notice", array("id" => $id));
				break;
			case "recoveryPage":   //恢复单页面
				$data["status"] = $status;
				return $this->changeFieldData("Notice", array("id" => $id), $data);
				break;
		}
	}
	
	/**
	 * 编辑
	 */
	public function edit() {
		$id = I("id");
		if (IS_POST) {
			$singlePage = D("Notice");
			if ($singlePage->create()) {
				if ($singlePage->save()) {
					$this->success("操作成功",Cookie('__forward__'));
				} else {
					$this->error("操作失败");
				}
			} else {
				$this->error($singlePage->getError());
			}
		}
	
		$info = D("Notice")->where(array("id" => $id))->find();
		$this->assign("info", $info);
		$index_path = C("index_tmpl_path");
		$index_path = $index_path . CONTROLLER_NAME . "/*";
		/** 获取模板文件 * */
		$theme_path_tmpl = array_map("basename", glob($index_path));
		$this->assign("theme_path_tmpl", $theme_path_tmpl);
		$this->meta_title = '页面编辑';
		$this->display();
	}
	/**
	 *  回收站
	 */
	public function trash() {
		$title=I('title');
		if($title)
		{
			$list = $this->lists("Notice", ['status' => -1,'title'=>['like','%'.$title.'%']]);
			$this->assign('title',$title);
		}  else {
			$list = $this->lists("Notice",['status' => -1]);
		}
	
		$this->assign("_list",$list);
		$this->meta_title = '页面列表';
		Cookie('__forward__', $_SERVER['REQUEST_URI']);
		return $this->display();
	}
	
	/**
	 * 数据强制删除
	 */
	public function del($id) {
	
		if (D("Notice")->where(array("id"=>$id))->delete()) {
			return $this->success("删除成功");
		} else {
			return $this->error("删除失败");
		}
	}
	
	public function delAll(){
		$ids=I("id");
		if(!empty($ids)){
			$ids_new=array();
			$ids_new=array_unique($ids);
			foreach($ids_new as $k=>$v){
				$this->delete("Notice", array("id" => $v));
			}
			$this->success("操作成功");
		}else{
			$this->error("操作失败");
		}
		 
	}
	
	function get_redirect_url($url){
		$redirect_url = null;
	
		$url_parts = @parse_url($url);
		if (!$url_parts) return false;
		if (!isset($url_parts['host'])) return false; //can't process relative URLs
		if (!isset($url_parts['path'])) $url_parts['path'] = '/';
	
		$sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
		if (!$sock) return false;
	
		$request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
		$request .= 'Host: ' . $url_parts['host'] . "\r\n";
		$request .= "Connection: Close\r\n\r\n";
		fwrite($sock, $request);
		$response = '';
		while(!feof($sock)) $response .= fread($sock, 8192);
		fclose($sock);
	
		if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
			if ( substr($matches[1], 0, 1) == "/" )
				return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
				else
					return trim($matches[1]);
	
		} else {
			return false;
		}
	
	}
   public  function curl_post_302($url,$data=null) {
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_URL, $url);
    	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 获取转向后的内容
    	$data = curl_exec($ch);
    	$Headers = curl_getinfo($ch);
    	curl_close($ch);
    	if($data != $Headers){
    		return $Headers["url"];
    	}else{
    		return false;
    	}
    }
   
}
